package com.junmo.fanbizhidemo.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.junmo.fanbizhidemo.R;

/**
 * Created by huruihua on 2017/8/24.
 */

public class MyBottomLayout extends LinearLayout {
    //跟布局是RelativeLayout
    private RelativeLayout homeLayout, selectLayout, searchLayout, locationLayout, settingLayout;
    //布局加载
    private LayoutInflater inflater;

    /**
     * 切换页面的接口
     */
    public interface ICallbackListener {
        public void clic(int id);
    }

    ICallbackListener iCallbackListener = null;

    public void setonCallbackListener(ICallbackListener iCallbackListener) {
        this.iCallbackListener = iCallbackListener;
    }

    public MyBottomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.layout_bottom, this);
        findView(view);
        initData();
        setonClick();
    }


    private void initData() {
        homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
        TextView tvHome = (TextView) homeLayout.findViewById(R.id.tabText);
        tvHome.setText("首页");
        tvHome.setTextColor(Color.BLUE);

        selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
        TextView tvSelect = (TextView) selectLayout.findViewById(R.id.tabText);
        tvSelect.setText("精选");
        tvSelect.setTextColor(Color.WHITE);

        searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
        TextView tvSearch = (TextView) searchLayout.findViewById(R.id.tabText);
        tvSearch.setText("搜索");
        tvSearch.setTextColor(Color.WHITE);

        locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
        TextView tvLoaction = (TextView) locationLayout.findViewById(R.id.tabText);
        tvLoaction.setText("本地");
        tvLoaction.setTextColor(Color.WHITE);

        settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
        TextView tvSetting = (TextView) settingLayout.findViewById(R.id.tabText);
        tvSetting.setText("设置");
        tvSetting.setTextColor(Color.WHITE);
    }

    private void findView(View view) {
        homeLayout = (RelativeLayout) view.findViewById(R.id.homeLayout);
        selectLayout = (RelativeLayout) view.findViewById(R.id.selectLayout);
        searchLayout = (RelativeLayout) view.findViewById(R.id.searchLayout);
        locationLayout = (RelativeLayout) view.findViewById(R.id.locationLayout);
        settingLayout = (RelativeLayout) view.findViewById(R.id.settingLayout);
    }

    private void setonClick() {
        homeLayout.setOnClickListener(new lister());
        selectLayout.setOnClickListener(new lister());
        searchLayout.setOnClickListener(new lister());
        locationLayout.setOnClickListener(new lister());
        settingLayout.setOnClickListener(new lister());
    }

    /**
     * 点击接口
     */
    private class lister implements OnClickListener {

        /**
         * 点击后改变点击状态
         * 切换页面
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.homeLayout:
                    initPix(0);
                    break;
                case R.id.selectLayout:
                    initPix(1);
                    break;
                case R.id.searchLayout:
                    initPix(2);
                    break;
                case R.id.locationLayout:
                    initPix(3);
                    break;
                case R.id.settingLayout:
                    initPix(4);
                    break;
            }
            iCallbackListener.clic(v.getId());
        }
    }

    /**
     * 切换卡的位置
     */
    public void initPix(int i) {
        switch (i) {
            case 0:
                homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvHome0 = (TextView) homeLayout.findViewById(R.id.tabText);
                tvHome0.setTextColor(Color.BLUE);

                selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSelect0 = (TextView) selectLayout.findViewById(R.id.tabText);
                tvSelect0.setTextColor(Color.WHITE);

                searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSearch0 = (TextView) searchLayout.findViewById(R.id.tabText);
                tvSearch0.setTextColor(Color.WHITE);

                locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvLocation0 = (TextView) locationLayout.findViewById(R.id.tabText);
                tvLocation0.setTextColor(Color.WHITE);

                settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSetting0 = (TextView) settingLayout.findViewById(R.id.tabText);
                tvSetting0.setTextColor(Color.WHITE);
                break;
            case 1:
                homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvHome1 = (TextView) homeLayout.findViewById(R.id.tabText);
                tvHome1.setTextColor(Color.WHITE);

                selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSelect1 = (TextView) selectLayout.findViewById(R.id.tabText);
                tvSelect1.setTextColor(Color.BLUE);

                searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSearch1 = (TextView) searchLayout.findViewById(R.id.tabText);
                tvSearch1.setTextColor(Color.WHITE);

                locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvLocation1 = (TextView) locationLayout.findViewById(R.id.tabText);
                tvLocation1.setTextColor(Color.WHITE);

                settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSetting1 = (TextView) settingLayout.findViewById(R.id.tabText);
                tvSetting1.setTextColor(Color.WHITE);
                break;
            case 2:

                homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvHome2 = (TextView) homeLayout.findViewById(R.id.tabText);
                tvHome2.setTextColor(Color.WHITE);

                selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSelect2 = (TextView) selectLayout.findViewById(R.id.tabText);
                tvSelect2.setTextColor(Color.WHITE);

                searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSearch2 = (TextView) searchLayout.findViewById(R.id.tabText);
                tvSearch2.setTextColor(Color.BLUE);

                locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvLocation2 = (TextView) locationLayout.findViewById(R.id.tabText);
                tvLocation2.setTextColor(Color.WHITE);

                settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSetting2 = (TextView) settingLayout.findViewById(R.id.tabText);
                tvSetting2.setTextColor(Color.WHITE);
                break;
            case 3:
                homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvHome3 = (TextView) homeLayout.findViewById(R.id.tabText);
                tvHome3.setTextColor(Color.WHITE);

                selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSelect3 = (TextView) selectLayout.findViewById(R.id.tabText);
                tvSelect3.setTextColor(Color.WHITE);

                searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSearch3 = (TextView) searchLayout.findViewById(R.id.tabText);
                tvSearch3.setTextColor(Color.WHITE);

                locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvLocation3 = (TextView) locationLayout.findViewById(R.id.tabText);
                tvLocation3.setTextColor(Color.BLUE);

                settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSetting3 = (TextView) settingLayout.findViewById(R.id.tabText);
                tvSetting3.setTextColor(Color.WHITE);

                break;
            case 4:
                homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvHome4 = (TextView) homeLayout.findViewById(R.id.tabText);
                tvHome4.setTextColor(Color.WHITE);

                selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSelect4 = (TextView) selectLayout.findViewById(R.id.tabText);
                tvSelect4.setTextColor(Color.WHITE);

                searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSearch4 = (TextView) searchLayout.findViewById(R.id.tabText);
                tvSearch4.setTextColor(Color.WHITE);

                locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvLocation4 = (TextView) locationLayout.findViewById(R.id.tabText);
                tvLocation4.setTextColor(Color.WHITE);

                settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.ic_launcher);
                TextView tvSetting4 = (TextView) settingLayout.findViewById(R.id.tabText);
                tvSetting4.setTextColor(Color.BLUE);
                break;

        }
    }

}
